/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.util;

import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PdfUtil {

    private final static Logger logger= LoggerFactory.getLogger(PdfUtil.class);
    public static final String FONT_SEGUISYM_PATH = "C:/Windows/Fonts/seguisym.ttf";

    public static Font getFont(int size, int style) {
        Font font = null;
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            font = new Font(baseFont, size, style);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return font;
    }

    public static Font getFont(String fontPath,int size, int style) {
        Font font = null;
        try {
            BaseFont baseFont = BaseFont.createFont(fontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            font = new Font(baseFont, size, style);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return font;
    }

    public static PdfPCell createCell(String content, Font font, int horizontalAlignment, int rowSpan, int colSpan) {
        return createCell(content, font, horizontalAlignment, rowSpan, colSpan, 0f);
    }

    public static PdfPCell createCell(String content, Font font, int horizontalAlignment, int rowSpan, int colSpan, float paddingBottom) {
        return createCell(new Paragraph(content, font), horizontalAlignment, rowSpan, colSpan, paddingBottom);
    }

    public static PdfPCell createCell(Paragraph paragraph, int horizontalAlignment, int rowSpan, int colSpan, float paddingBottom) {
        PdfPCell pdfCell = new PdfPCell(paragraph);
        pdfCell.setHorizontalAlignment(horizontalAlignment);
        pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        if (rowSpan > 1) {
            pdfCell.setRowspan(rowSpan);
        }
        if (colSpan > 1) {
            pdfCell.setColspan(colSpan);
        }
        if (paddingBottom > 0) {
            pdfCell.setPaddingBottom(paddingBottom);
        }
        return pdfCell;
    }

    public static Paragraph createParagraph(String content, Font font, int alignment) {
        Paragraph paragraph = new Paragraph(content, font);
        paragraph.setAlignment(alignment);
        return paragraph;
    }

    public static PdfPCell createCell(Image image, boolean fit, int horizontalAlignment, int rowSpan, int colSpan, float paddingBottom) {
        PdfPCell pdfCell = new PdfPCell(image, fit);
        pdfCell.setHorizontalAlignment(horizontalAlignment);
        pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        if (rowSpan > 1) {
            pdfCell.setRowspan(rowSpan);
        }
        if (colSpan > 1) {
            pdfCell.setColspan(colSpan);
        }
        if (paddingBottom > 0) {
            pdfCell.setPaddingBottom(paddingBottom);
        }
        return pdfCell;
    }



}
